﻿using Kz.Telegram.Bot.Infrastructure.Extensions;
using System.Net;

namespace Kz.Telegram.Bot.Infrastructure.Http
{
    public record ApiResponse
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 提示内容
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 请求的ID
        /// </summary>
        public string RequestId { get; set; }

        /// <summary>
        /// 默认构造
        /// </summary>
        /// <param name="code"></param>
        public ApiResponse(HttpStatusCode code = HttpStatusCode.OK)
        {
            Code = (int)code;
            Message = code.GetDesc();
        }

        public ApiResponse(bool success, string msg)
        {
            Code = (int)HttpStatusCode.OK;
            Message = msg;
        }

        public ApiResponse(HttpStatusCode code, string msg)
        {
            Code = (int)code;
            Message = msg;
        }

        public static ApiResponse Fail(string msg)
        {
            return new ApiResponse(HttpStatusCode.InternalServerError, msg);
        }

        public static ApiResponse Fail(HttpStatusCode errorCode, string msg)
        {
            return new ApiResponse(errorCode, msg);
        }

        public static ApiResponse Fail(HttpStatusCode errorCode = HttpStatusCode.InternalServerError)
        {
            return new ApiResponse(errorCode);
        }

        public static ApiResponse Succes()
        {
            return new ApiResponse(HttpStatusCode.OK);
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResponse<T> Succes<T>(T data = default)
        {
            return new ApiResponse<T>(HttpStatusCode.OK, data);
        }

        public static ApiResponse<T> Fail<T>(HttpStatusCode parameterValidationError)
        {
            throw new NotImplementedException();
        }
    }


    public record ApiResponse<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 提示内容
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 返回数据
        /// </summary>
        public T Data { get; set; } = default;

        /// <summary>
        /// 请求的ID
        /// </summary>
        public string RequestId { get; set; }

        /// <summary>
        /// 默认构造
        /// </summary>
        /// <param name="code"></param>
        /// <param name="data"></param>
        public ApiResponse(HttpStatusCode code, T data = default)
        {
            Code = (int)code;
            Message = code.GetDesc();
            Data = data ?? default;
        }

        private ApiResponse(HttpStatusCode code, string msg)
        {
            Code = (int)code;
            Message = msg;
        }

        private ApiResponse(HttpStatusCode code, string msg, T data = default)
        {
            Code = (int)code;
            Message = msg;
            Data = data ?? default;
        }

        public static ApiResponse<T> Succes(T data = default)
        {
            return new ApiResponse<T>(HttpStatusCode.OK, data);
        }

        public static ApiResponse<T> Fail(T msg)
        {
            return new ApiResponse<T>(HttpStatusCode.InternalServerError, msg);
        }

        public static ApiResponse<T> Fail(string msg)
        {
            return new ApiResponse<T>(HttpStatusCode.InternalServerError, msg);
        }


        public static ApiResponse<T> Fail(HttpStatusCode errorCode = HttpStatusCode.InternalServerError)
        {
            return new ApiResponse<T>(errorCode);
        }

        public static ApiResponse<T> Fail(HttpStatusCode errorCode, string msg, T data = default)
        {
            return new ApiResponse<T>(errorCode, msg, data);
        }
    }
}